<?php

namespace App\Api\Controllers;

use App\Api\Models\CompanyClassModel;
use App\Api\Services\CompanyClassService;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class CompanyClassController extends BaseController
{
    /**
     * 行业类型列表
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/15 16:11
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $model = CompanyClassModel::select('id','name','image','order')->orderBy('order','asc')->get();
        return success($model);
    }

    /**
     * 新建公司类型
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/16 9:48
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'name' =>'required',//类型名称
            'image' =>'required',//图片地址
            'order' =>'required|integer',//排序
        ],[
            'name.required' => '请输入类型名称',
            'image.required' => '请上传图片',
            'order.required' => '请设置排序',
            'order.integer' => '排序仅能为数字',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        $checkClass = CompanyClassService::checkClass($data);
        if(!$checkClass) {
            return $this->fail('', '公司行业类型已存在');
        }

        $this->log('添加记账分类',$data);
        $data['created_at'] = time();
        return $this->success(CompanyClassModel::insertGetId($data));
    }

    /**
     * 编辑
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/16 14:04
     * @return \Illuminate\Http\JsonResponse|mixed
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
            'name' =>'required',//类型名称
            'image' =>'required',//图片地址
            'order' =>'required|integer',//排序
        ],[
            'id.required' => '参数`id`标识缺失',
            'id.integer' => '参数`id`仅能为数字',
            'name.required' => '参数`公司名称`缺失',
            'image.required' => '参数`图片`缺失',
            'order.required' => '请设置排序',
            'order.integer' => '排序仅能为数字',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        unset($data['id']);
        $result = CompanyClassModel::where($data)->where('id', '!=', $request->id)->first();

        if(!empty($result)) {
            return $this->fail('', '公司行业类型已存在');
        }

        CompanyClassModel::where('id', $request->id)->update($request->all());
        $this->log('修改公司类型', $data);
        return $this->success();
    }

    /**
     * 删除
     * @param Request $request
     * AUTHOR  : Sun Wu
     * DateTime: 2022/3/16 14:12
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//企业类型主键id
        ],[
            'id.required' => '参数`id`标识缺失',
            'id.integer' => '参数`id`仅能为数字',
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return $this->fail('', $info);
        }

        CompanyClassModel::destroy($data['id']);
        return $this->success();
    }
}
